package com.intelligent.ispc.client.core.view.system;

import com.intelligent.ispc.client.core.controller.ChangePwdController;
import com.intelligent.ispc.client.core.validator.GroupKeys;
import com.intelligent.ispc.client.core.validator.SysChangePwdDto;
import com.intelligent.ispc.client.core.view.AbstractPanel;
import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.client.utils.ResourceMessages;
import com.intelligent.ispc.client.utils.validation.GuiComponentFactory;
import com.intelligent.ispc.client.utils.validation.ValidationUI;
import com.intelligent.ispc.core.dto.UserDto;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.jxlayer.JXLayer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by simon.liu on 2016/9/13.
 */
@Component
public class ChangePwdPanel extends AbstractPanel {

    @Autowired
    private ChangePwdController changePwdController;

    @Override
    public void initDataSource() {
    }

    @Override
    public void initView() {
    }

    @PostConstruct
    @Override
    public void initComponents() {
        sysChangePwdDto = new SysChangePwdDto();
        chanPwdPanel = new JPanel();

        oldPwdLable = new JLabel();
        oldPwdText = GuiComponentFactory.createPasswordTextField(new BindingGroup(), sysChangePwdDto, SysChangePwdDto.PROPERTYNAME_OLDPASSWORD);
        ((ValidationUI) oldPwdText.getUI()).setGroup(GroupKeys.class);
        sysChangePwdDto.setOldPassword(null);
        newPwdLable = new JLabel();
        newPwdText = GuiComponentFactory.createPasswordTextField(new BindingGroup(), sysChangePwdDto, SysChangePwdDto.PROPERTYNAME_NEWPASSWORD);
        ((ValidationUI) newPwdText.getUI()).setGroup(GroupKeys.class);
        sysChangePwdDto.setNewPassword(null);
        confPwdLable = new JLabel();
        confPwdText = GuiComponentFactory.createPasswordTextField(new BindingGroup(), sysChangePwdDto, SysChangePwdDto.PROPERTYNAME_CONFIRMPASSWORD);
        ((ValidationUI) confPwdText.getUI()).setGroup(GroupKeys.class);
        sysChangePwdDto.setConfirmPassword(null);
        saveButton = new JButton();

        oldPwdLable.setText("Old password:");
        newPwdLable.setText("New password:");
        confPwdLable.setText("Confirm password:");
        saveButton.setText("Save");
        addSubComponent();

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
    }

    private void addSubComponent() {
        this.setLayout(new GridBagLayout());
        chanPwdPanel.setLayout(new GridBagLayout());
        chanPwdPanel.setBorder(BorderFactory.createTitledBorder("Change Password"));
        chanPwdPanel.add(oldPwdLable, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(10, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(oldPwdText, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(60, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(new JPanel(), new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(30, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(newPwdLable, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(10, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(newPwdText, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(60, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(new JPanel(), new GBC(2, 1).setFill(GBC.HORIZONTAL).setWeight(30, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(confPwdLable, new GBC(0, 2).setFill(GBC.HORIZONTAL).setWeight(10, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(confPwdText, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(60, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(new JPanel(), new GBC(2, 2).setFill(GBC.HORIZONTAL).setWeight(30, 0).setInsets(5, 0, 0, 0));
        chanPwdPanel.add(saveButton, new GBC(1, 3).setInsets(5, 0, 0, 0).setAnchor(GBC.WEST));
        //this.add(new JPanel(),new GBC(0,4).setFill(GBC.BOTH).setWeight(0,90));
        this.add(chanPwdPanel, new GBC(0, 0).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTH).setWeight(100, 100));
        this.setVisible(true);
    }

    private void saveActionPerformed(ActionEvent evt) {

        if ("nullValue".equals(inputValueConf())) {
            // JOptionPane.showMessageDialog(null,"Please input your information at first!","Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        if ("pwdLenNotOk".equals(inputValueConf())) {
            //JOptionPane.showMessageDialog(null,"New password length is not appropriate,\n at least six-character!","Warning",JOptionPane.WARNING_MESSAGE);
            return;
        }
        if ("confPwdError".equals(inputValueConf())) {
            JOptionPane.showMessageDialog(null, "New password confirm failure !", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if ("oldPwdError".equals(inputValueConf())) {
            JOptionPane.showMessageDialog(null, " Your old password is error !", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        UserDto userDto = ResourceMessages.userMap.get("user");
        userDto.setPassword(newPwdText.getView().getText());
        changePwdController.updateUserInfo(userDto);
        JOptionPane.showMessageDialog(null, "       Change password succeed !", "Message", JOptionPane.PLAIN_MESSAGE);
    }

    private String inputValueConf() {
        String oldPwd = oldPwdText.getView().getText();
        String newPwd = newPwdText.getView().getText();
        String confPwd = confPwdText.getView().getText();
        int length = newPwd.length();

        if (!changePwdController.isOldPwdOk(oldPwd)) {
            return "oldPwdError";
        }
        if ("".equals(oldPwd) || "".equals(newPwd) || "".equals(confPwd)) {
            return "nullValue";
        }
        if (length < 6 || length > 12) {
            return "pwdLenNotOk";
        }
        if (!newPwd.equals(confPwd)) {
            return "confPwdError";
        }

        return null;
    }


    private JLabel oldPwdLable;
    private JXLayer<JTextComponent> oldPwdText;
    private JLabel newPwdLable;
    private JXLayer<JTextComponent> newPwdText;
    private JLabel confPwdLable;
    private JXLayer<JTextComponent> confPwdText;
    private JButton saveButton;

    private JPanel chanPwdPanel;
    private SysChangePwdDto sysChangePwdDto;
}
